



#include "tls.h"
#include "types.h"
#include <string.h>
#include <stdlib.h>
/**
 * @fn      
 * @brief 
 
 test code:
 ---------------------------------------------------
 def prvAlignSizeGet(usSize):
	ucCmpValue = usSize & 0x03

	if ucCmpValue == 0:
		print("input:{} output:{}".format(usSize ,usSize ) )
		print('\r\n')
	else:
		print("input:{} output:{}".format(usSize ,(4 -ucCmpValue) + usSize ) )

a=range(1,100)

for i in a:
	prvAlignSizeGet(i)
---------------------------------------------------
 * @param   
 * @return  */
uint16 tlsAlignSizeGet(uint16 usSize)
{
	uint8 ucCmpValue = usSize & 0x03;

	if (ucCmpValue == 0) {
		return usSize;
	}else {
		return ((4 - ucCmpValue) + usSize);
	}
}

/**
 * @fn      
 * @brief  get a hash value. 0-256
 * @param   
 * @return  */
uint8 tlsStringHashValueGet(char *pcString)
{
	uint8 ucHash = 0;

	uint16 ucSize;

	/* String length. */
	ucSize = strlen(pcString);

	/* Add value. */
	for (uint8 i = 0; i < ucSize;i++) {
		ucHash += pcString[i];
	}

	/*if (ucHash == 65535){
		ucHash -= 10;
	}*/

	return ucHash;
}

/*#############################################*/
/**
* @fn  
* @brief string to uint8
* @param   
* @return   */
uint8 hex2uint8(char cHigh , char cLow)
{
	uint8 ucVal =0;

	/* High transformation */
	if(cHigh >= '0' && cHigh <= '9'){
		cHigh = cHigh - '0';
	}
	else if(cHigh >='a' && cHigh <= 'f'){
		cHigh = cHigh - 'a' + 10;
	}
	else if(cHigh >= 'A' && cHigh <= 'F'){
		cHigh = cHigh - 'A' + 10;
	}

	/* Low transformation */
	if(cLow >= '0' && cLow <= '9'){
		cLow = cLow - '0';
	}
	else if(cLow >='a' && cLow <= 'f'){
		cLow = cLow - 'a' + 10;
	}
	else if(cLow >= 'A' && cLow <= 'F'){
		cLow = cLow - 'A' + 10;
	}

	ucVal = cHigh << 4 ;
	ucVal += cLow;

	return ucVal;
}

/**
* @fn  
* @brief 
* @param   
* @return   */
void prvByteToHex(char *pcbuf ,uint8 ucByte)
{
    const char ucHexTab[] = {
        '0', '1', '2', '3', '4', '5', 
		'6', '7', '8', '9', 'A', 'B',
		'C', 'D', 'E', 'F'
    };

    pcbuf[0] = ucHexTab[ucByte>>4];
    pcbuf[1] = ucHexTab[ucByte & 0x0F];
}

/**
* @fn  
* @brief sting 2 number.
* @param   
* @return   */
uint32 tlsStr2Num(char *pcSeq)
{
	char* pcStop;
	return strtol(pcSeq ,&pcStop , 10) ;
}

/**
* @fn  
* @brief example input: 1F990563
		 Little Endian
* @param   
* @return   */
uint32 tlsHex2Dec(char *strHex)
{
	uint8  ucSize  = strlen(strHex);
	uint32 ulRes  = 0 ;

//	LOG("strlen length:%d\r\n" , ucSize);

	if((ucSize & 0x01) == 0){
		ucSize /=2;
		for(uint8 i=ucSize; i> 0 ;i--){
			ulRes <<= 8 ;
			ulRes += hex2uint8(strHex[2*(i-1)],strHex[2*(i-1)+1]);
		}
	}

	return ulRes;
}

/**
* @fn  
* @brief Little Endian
* @param   
* @return   */
void tlsU32toHex(uint32 ulVal ,char* ptrHex)
{
	prvByteToHex(&ptrHex[0] , ulVal);
	prvByteToHex(&ptrHex[2] , ulVal>>8);
	prvByteToHex(&ptrHex[4] , ulVal>>16);
	prvByteToHex(&ptrHex[6] , ulVal>>24);

	ptrHex[8] = '\0';
}

/**
* @fn  
* @brief 
* @param   
* @return   */
void tlsU16toHex(uint16 usVal ,char* ptrHex)
{
	prvByteToHex(&ptrHex[0] , usVal);
	prvByteToHex(&ptrHex[2] , usVal>>8);
	ptrHex[4] = '\0';
}

/**
* @fn  
* @brief 
* @param   
* @return   */
void tlsStrDel(char *str , char c)
{
	int i=0; 
	int j=0;

	for(;str[i] != '\0' ;i++){
		if(str[i]!=c){
			str[j++] = str[i];
		}
	}

	str[j] = '\0';
}









